#include "stdafx.h"

#include "Joystick.h"

int SharpSDL::ManagedSDL::Joystick::JoystickNum(void)
{
	return SDL_NumJoysticks();
}

void SharpSDL::ManagedSDL::Joystick::Open(int index)
{
	if ((index < JoystickNum()) && (index >= 0))
	{
		CurrentJoystick = SDL_JoystickOpen(index);
		if ((SDL_JoystickOpened(index)) && (SDL_JoystickIndex(CurrentJoystick) == index))
		{
			Status = JoystickStatus::Opened;
			JoystickIndex = index;
			Axis = SDL_JoystickNumAxes(CurrentJoystick);
			Buttons = SDL_JoystickNumButtons(CurrentJoystick);
			TrackBalls = SDL_JoystickNumBalls(CurrentJoystick);
			Hats = SDL_JoystickNumHats(CurrentJoystick);
		} else {
			SDL_Error((SDL_errorcode)90);
			Status = JoystickStatus::Closed;
			JoystickIndex = -1;
		}
	} else {
		SDL_Error((SDL_errorcode)91);
		Status = JoystickStatus::Closed;
		JoystickIndex = -1;
	}
}

SharpSDL::ManagedSDL::Joystick::Joystick(int index)
{
	Open(index);
}

System::String ^SharpSDL::ManagedSDL::Joystick::GetJoystickName(void)
{
	System::String ^ret;
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
		ret = gcnew System::String((char*)SDL_JoystickName(JoystickIndex));
	else
		ret = gcnew System::String("No Joystick");
	return ret;
}

int SharpSDL::ManagedSDL::Joystick::GetJoystickIndex(void)
{
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
		return JoystickIndex;
	else
		return -1;
}

SharpSDL::ManagedSDL::Joystick::JoystickStatus SharpSDL::ManagedSDL::Joystick::GetJoystickStatus()
{
		return Status;
}

int SharpSDL::ManagedSDL::Joystick::GetHatsNumber(void)
{
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
		return Hats;
	else
		return -1;
}

int SharpSDL::ManagedSDL::Joystick::GetButtonsNumber(void)
{
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
		return Buttons;
	else
		return -1;
}

int SharpSDL::ManagedSDL::Joystick::GetAxisNumber(void)
{
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
		return Axis;
	else
		return -1;
}

int SharpSDL::ManagedSDL::Joystick::GetTrackBallsNumber(void)
{
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
		return TrackBalls;
	else
		return -1;
}

void SharpSDL::ManagedSDL::Joystick::SetJoystickEvent(SharpSDL::ManagedSDL::Joystick::JoystickEvent AutoJoystickUpdate)
{
	int newstate;
	if (AutoJoystickUpdate == SharpSDL::ManagedSDL::Joystick::JoystickEvent::Enabled)
		newstate = SDL_ENABLE;
	else
		newstate = SDL_IGNORE;
	SDL_JoystickEventState(newstate);
}

SharpSDL::ManagedSDL::Joystick::JoystickEvent SharpSDL::ManagedSDL::Joystick::GetJoystickEvent(void)
{
	if (SDL_JoystickEventState(SDL_QUERY) == SDL_ENABLE)
		return JoystickEvent::Enabled;
	else
		return JoystickEvent::Disabled;
}

void SharpSDL::ManagedSDL::Joystick::UpdateJoystick(void)
{
	SDL_JoystickUpdate();
	return;
}

int SharpSDL::ManagedSDL::Joystick::GetAxeStatus(int axe)
{
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
	{
		if ((axe < Axis) && (axe >= 0))
			return (int)SDL_JoystickGetAxis(CurrentJoystick, axe);
		else
			return 0;
	} else {
		return 0;
	}
}

SharpSDL::ManagedSDL::Joystick::HatStatus SharpSDL::ManagedSDL::Joystick::GetHatStatus(int hat)
{
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
	{
		if ((hat < Hats) && (hat >= 0))
			return (SharpSDL::ManagedSDL::Joystick::HatStatus)SDL_JoystickGetHat(CurrentJoystick, hat);
		else
			return (SharpSDL::ManagedSDL::Joystick::HatStatus)-1;
	} else {
		return (SharpSDL::ManagedSDL::Joystick::HatStatus)-1;
	}
}

SharpSDL::ManagedSDL::Joystick::ButtonStatus  SharpSDL::ManagedSDL::Joystick::GetButtonStatus(int button)
{
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
	{
		if ((button < Buttons) && (button >= 0))
		{
			switch (SDL_JoystickGetButton(CurrentJoystick, button))
			{
			case 0:
				return SharpSDL::ManagedSDL::Joystick::ButtonStatus::ButtonUP;
				break;
			case 1:
				return SharpSDL::ManagedSDL::Joystick::ButtonStatus::ButtonDOWN;
				break;
			default:
				return SharpSDL::ManagedSDL::Joystick::ButtonStatus::ButtonError;
				break;
			}
		} else {
			return SharpSDL::ManagedSDL::Joystick::ButtonStatus::ButtonError;
		}
	} else {
		return SharpSDL::ManagedSDL::Joystick::ButtonStatus::ButtonError;
	}
}

void SharpSDL::ManagedSDL::Joystick::Close(void)
{
	if (Status == SharpSDL::ManagedSDL::Joystick::JoystickStatus::Opened)
	{
		SDL_JoystickClose(CurrentJoystick);
		Status = SharpSDL::ManagedSDL::Joystick::JoystickStatus::Closed;
	} else {
		return;
	}
}

SharpSDL::ManagedSDL::Joystick::~Joystick(void)
{
	Close();
}